﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;


using ConstructionBidOnline.DAL;
using ConstructionBidOnline.DAL.dsWorkStatusTableAdapters;
using ConstructionBidOnline.Common;
namespace ConstructionBidOnline.Biz
{
    public class WorkStatus : BOBase
    {
        #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected WorkStatus() { }

        #endregion

        #region Public Properties

        #region Instance Properties

        /// <summary>
        /// PKID of the WorkStatus.
        /// </summary>
        public int WorkStatusID
        {
            get { return _workStatusID; }
            set
            {
                if (_workStatusID != value)
                {
                    if (OnPropertyChanging("WorkStatusID", _workStatusID, value))
                    {
                        int oldValue = _workStatusID;
                        _workStatusID = value;

                        OnPropertyChanged("WorkStatusID", oldValue, value);
                    }
                }
            }
        }  private int _workStatusID = -1;

        /// <summary>
        /// ContractID.
        /// </summary>
        public string ContractID
        {
            get { return _contractID; }
            set
            {
                if (_contractID != value)
                {
                    if (OnPropertyChanging("ContractID", _contractID, value))
                    {
                        string oldValue = _contractID;
                        _contractID = value;
                        OnPropertyChanged("ContractID", oldValue, value);
                    }
                }
            }
        }  private string _contractID = null;


        /// <summary>
        /// StartingImage.
        /// </summary>
        public string StartingImage
        {
            get { return _startingImage; }
            set
            {
                if (_startingImage != value)
                {
                    if (OnPropertyChanging("StartingImage", _startingImage, value))
                    {
                        string oldValue = _startingImage;
                        _startingImage = value;
                        OnPropertyChanged("StartingImage", oldValue, value);
                    }
                }
            }
        }  private string _startingImage = null;




        /// <summary>
        /// EndingImage.
        /// </summary>
        public string EndingImage
        {
            get { return _endingImage; }
            set
            {
                if (_endingImage != value)
                {
                    if (OnPropertyChanging("EndingImage", _endingImage, value))
                    {
                        string oldValue = _endingImage;
                        _endingImage = value;
                        OnPropertyChanged("EndingImage", oldValue, value);
                    }
                }
            }
        }  private string _endingImage = null;

        
        /// <summary>
        /// Date/time at which the work status was posted.
        /// </summary>
        public DateTime? AddedDate
        {
            get { return _addedDate; }
            set
            {
                if (_addedDate != value)
                {
                    if (OnPropertyChanging("AddedDate", _addedDate, value))
                    {
                        DateTime? oldValue = _addedDate;
                        _addedDate = value;
                        OnPropertyChanged("AddedDate", oldValue, value);
                    }
                }
            }
        }  private DateTime? _addedDate = null;



        /// <summary>
        /// Description.
        /// </summary>
        public string Description
        {
            get { return _description; }
            set
            {
                if (_description != value)
                {
                    if (OnPropertyChanging("Description", _description, value))
                    {
                        string oldValue = _description;
                        _description = value;
                        OnPropertyChanged("Description", oldValue, value);
                    }
                }
            }
        }  private string _description = null;


        #endregion

        #endregion

        #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty WorkStatus object.
        /// </summary>
        public static WorkStatus CreateNew()
        {
            WorkStatus obj = new WorkStatus();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;
            obj.AddedDate = DateTime.Now;
            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static WorkStatus CreateNew(WorkStatusCollection collection)
        {
            WorkStatus obj = CreateNew();

            // Get next/unique ID for the new object.
            int minVal = 0;
            foreach (WorkStatus workStatus in collection)
            {
                if (workStatus.WorkStatusID < minVal)
                    minVal = workStatus.WorkStatusID;
            }
            obj.WorkStatusID = --minVal;
            collection.Add(obj);

            return (obj);
        }

        #endregion

        #region GetCompleteWorkStatusByContractID

        /// <summary>
        /// Returns a collection of WorkStatus.
        /// </summary>
        public static WorkStatusCollection GetCompleteWorkStatusByContractID(string contractid)
        {
            WorkStatusTableAdapter adapter = new WorkStatusTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsWorkStatus.WorkStatusDataTable tbl = adapter.GetData(contractid);

            WorkStatusCollection list = new WorkStatusCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsWorkStatus.WorkStatusRow row in tbl.Rows)
                {
                    WorkStatus directory = new WorkStatus();
                    RelationalToObject(directory, row);
                    list.Add(directory);
                }
            }

            return (list);
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the specified WorkStatus.
        /// </summary>
        public static void Update(WorkStatus obj)
        {
            bool updateWorkStatus = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updateWorkStatus = (obj.ObjState != ObjectState.Unchanged);

                if (!updateWorkStatus)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsWorkStatus.WorkStatusDataTable tbl = null;
            if (updateWorkStatus)
            {

                WorkStatusTableAdapter adapter = new WorkStatusTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();


                tbl = new dsWorkStatus.WorkStatusDataTable();
                dsWorkStatus.WorkStatusRow row = tbl.NewWorkStatusRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("WorkStatusId", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

            }

            return (Errors.Count == 0);
        }

        #endregion

        #region ToString

        /// <summary>
        /// User-friendly string represetation of this object.
        /// </summary>
        public override string ToString()
        {
            return (String.Format("WorkStatus: WorkStatusID={0}, ContractID={1}, {2}", WorkStatusID, ContractID, Description));
        }

        #endregion

        #endregion
    }

    /// <summary>
    /// Collection of 0 or more SubAdmin objects.
    /// </summary>

    public class WorkStatusCollection : List<WorkStatus>
    {

        #region Types

        public class UserComparer : IComparer<WorkStatus>
        {
            public int Compare(WorkStatus ws1, WorkStatus ws2)
            {
                return (String.Compare(ws1.ContractID, ws2.ContractID));
            }
        }

        #endregion

        #region Public Methods

        #region FindWorkStatusByContractID

        public WorkStatus FindWorkStatusByContractID(string ContractID)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].ContractID == ContractID)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #endregion

    }
}
